package action;

import beans.Account;
import dao.CheckAccount;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Register extends HttpServlet {

    String username = "";
    String password = "";
    String full_name = "";
    String gender = "";
    String address = "";
    String phone = "";
    String email = "";
    String roll = "";
    String acc_id = "";

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            CheckAccount ck = new CheckAccount();
            username = request.getParameter("username_st");
            password = request.getParameter("password_st");
            full_name = request.getParameter("full_name");
            gender = request.getParameter("gender");
            address = request.getParameter("address");
            phone = request.getParameter("phone");
            email = request.getParameter("email");
            roll = request.getParameter("roll");
            String msg = "";
            if (!username.equals("") && username != null) {
                if (!password.equals("") && password != null) {
                    if (!ck.checkUsername(username)) {
                        if (!full_name.equals("") && full_name != null) {
                            Account account = new Account();
                            account.setUser_name(username);
                            account.setPass_word(password);
                            account.setFull_name(full_name);
                            account.setGender(gender);
                            account.setAddress(address);
                            account.setPhone(phone);
                            account.setEmail(email);
                            account.setRoll(roll);
                            if (ck.createAccount(account)) {                                
                                RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                                rd.forward(request, response);
                            } else {
                                msg = "Create error !";
                                request.setAttribute("msg", msg);
                                RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
                                rd.forward(request, response);
                            }
                        } else {
                            msg = "Insert full name";
                            request.setAttribute("msg", msg);
                            RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
                            rd.forward(request, response);
                        }
                    } else {
                        msg = "Username has exist !";
                        request.setAttribute("msg", msg);
                        RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
                        rd.forward(request, response);
                    }
                } else {
                    msg = "Insert password !";
                    request.setAttribute("msg", msg);
                    RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
                    rd.forward(request, response);
                }
            } else {
                msg = "Insert user name !";
                request.setAttribute("msg", msg);
                RequestDispatcher rd = request.getRequestDispatcher("register.jsp");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
